package com.ehouse.connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ConnectionManager {
	private String ip = "192.168.1.50";
	private int port = 6500;
	private SocketManager manager;

	public ConnectionManager() throws UnknownHostException, IOException {

		this.manager = new SocketManager(ip, port);

	}

	public ConnectionManager(String ip) throws UnknownHostException,
			IOException {
		this.ip = ip;
		this.manager = new SocketManager(ip, port);
	}

	public void closeConnection() {
		try {
			manager.closeConnection();
		} catch (Exception ex) {
		}
	}
	
	public String send(String text) throws IOException {
		return manager.send(text);
	}
	
	public void setIp(String ip) {
		System.out.println("SET IP");
		try {
			manager.send("SET_IP/" + ip);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setTime(String year, String mon, String day, String hour, String min, String sec) {
		System.out.println("SET TIME");
		try {
			manager.send("SET_TIME/" + year + "/" + mon + "/" + day + "/" + hour + "/" + min + "/" + sec);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getLights() {
		System.out.println("GET LIGHTS");
		try {
			return manager.send("GET_LIGHTS");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getWindows() {
		System.out.println("GET WINDOWS");
		try {
			return manager.send("GET_WINDOWS");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getTemperature() {
		System.out.println("GET TEMPERATURE");
		try {
			return manager.send("GET_TEMPERATURE");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getIrrigation() {
		System.out.println("GET IRRIGATION");
		try {
			return manager.send("GET_IRRIGATION");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String switchSensor(String sensor, String switchName, boolean state) {
		String sending = "SET_" + sensor.toUpperCase() + "/"
				+ switchName.toUpperCase() + "/"
				+ Boolean.toString(state).toUpperCase();
		sending = sending.replace("//", "/");
		System.out.println(sending);
		try {
			return manager.send(sending);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}

	private class SocketManager {
		private Socket socket;
		private PrintWriter printer;
		private BufferedReader reader;

		public SocketManager(String ip, int port) throws UnknownHostException,
				IOException {
			socket = new Socket(ip, port);
			printer = new PrintWriter(socket.getOutputStream());
			reader = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
		}

		public String send(String text) throws IOException {
			printer.print(text);
			printer.flush();
			char[] buffer = new char[1024];
			int read = reader.read(buffer);
			StringBuffer sb = new StringBuffer(read);
			for (int i = 0; i < read; i++) {
				sb.append(buffer[i]);
			}

			return sb.toString();
		}

		public void closeConnection() throws IOException {
			if (socket.isConnected()) {
				socket.close();
			}
		}
	}
}